import http from '../../utils/http'
// pages/personal/personal.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img: '../../image/tx.jpg',
    name: '',
    oldname: '',
    avatar: '',
    distinguish: false
  },

  syncCourt() {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    //   /userInfo
    this.gain()
  },

  gain() {
    http.get('/userInfo').then(res => {
      if (res.code === 10000) {
        let { avatar, nickName } = res.data
        this.setData({
          img: avatar ? avatar : this.data.img,
          avatar: avatar ? avatar : this.data.img,
          name: nickName ? nickName : '',
          oldname: nickName ? nickName : '',
        })
      }
    })
  },

  onChooseAvatar(e: any) {
    const { avatarUrl } = e.detail
    this.setData({
      avatar: avatarUrl,
      img: avatarUrl
    })
    this.alter()
  },
  blurinput() {
    this.createSelectorQuery().select('.input').fields({ properties: ['value'] }).exec(res => {
      console.log(res[0].value);
      this.setData({
        name: res[0].value
      })
      let { name, oldname } = this.data

      if (name === oldname) {
        return
      }

      this.setData({
        oldname: this.data.name
      })
      this.alter()
    })


  },

  chage(e: any) {
    this.setData({
      name: e.detail.value
    })
  },

  alter() {
    this.setData({
      distinguish: true
    })
    http.put('/userInfo', {
      nickName: this.data.name,
      avatar: this.data.avatar,
    })
  },    /**
     * 生命周期函数--监听页面初次渲染完成
     */
  onReady() {
    // let dquc = this.createSelectorQuery()
    // dquc.select('.image').boundingClientRect()
    // dquc.select('.image').node()
    // dquc.select('.image').fields({properties: ['src']})
    // dquc.selectViewport().scrollOffset()
    // dquc.exec(res => {
    //     console.log(res[2].src);
    // })
    // this.createSelectorQuery().select('.input').fields({ properties: ['value'] }, (res) => {
    //     console.log(res);
    // }).exec(res => {
    //     console.log(res);
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (this.data.distinguish) {
      wx.navigateBack({
        delta: 1,
        success: function () {
          let pages = getCurrentPages(); // 获取当前页面栈
          if (pages.length) {
            pages[pages.length - 1].gain() // 获取当前页面
          }
        }
      });
    }

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})